package com.example.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

/**
 * 日期工具类：为了保证SimpleDateFormath和Calendar的线程安全
 */
public class DateUtils {

    private static ThreadLocal<DateFormat> sdfThreadLocal = new ThreadLocal<DateFormat>() {
        @Override
        public SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    private static ThreadLocal<Calendar> calThreadLocal = new ThreadLocal<Calendar>() {
        @Override
        public Calendar initialValue() {
            return Calendar.getInstance();
        }
    };

    public static String formatDate(Date date) {
        try {
            return sdfThreadLocal.get().format(date);
        } catch (Exception e) {
            return null;
        }
    }

    public static Date parse(String strDate)  {
        try {
            return sdfThreadLocal.get().parse(strDate);
        } catch (Exception e) {
            return null;
        }
    }

    public static Long getWholeTimeStamp(){
        Date date = new Date();
        Calendar instance = calThreadLocal.get();
        instance.setTime(date);
        instance.set(Calendar.HOUR_OF_DAY,0);
        instance.set(Calendar.MINUTE,0);
        instance.set(Calendar.SECOND,0);
        instance.set(Calendar.MILLISECOND,0);
        return instance.getTimeInMillis();
    }
    public static List<String> findDates(Long dBegin, Long dEnd, String ft) throws ParseException, ParseException {
        //日期工具类准备
        DateFormat format = new SimpleDateFormat(ft);

        //设置开始时间

        Calendar calBegin = Calendar.getInstance();
        Date begin = new Date(dBegin);
        calBegin.setTime(begin);

        //设置结束时间
        Calendar calEnd = Calendar.getInstance();
        Date end = new Date(dEnd);
        calEnd.setTime(end);

        //装返回的日期集合容器
        List<String> Datelist = new ArrayList<String>();
        //将第一个月添加里面去
        Datelist.add(format.format(calBegin.getTime()));
        // 每次循环给calBegin日期加一天，直到calBegin.getTime()时间等于dEnd
        while (end.after(calBegin.getTime()))  {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            Datelist.add(format.format(calBegin.getTime()));
        }
        return Datelist;
    }

    public static String secondToDate(double second) {
        Long time =  new Long(new Double(second).longValue());
        String strTime = null;
        Long days = time / (60 * 60 * 24);
        Long hours = (time % (60 * 60 * 24)) / (60 * 60);
        Long minutes = (time % (60 * 60)) / 60;
        Long seconds = time % 60;
        if (days > 0) {
            strTime = days + "天" + hours + "小时" + minutes + "分钟";
        } else if (hours > 0) {
            strTime = hours + "小时" + minutes + "分钟";
        } else if (minutes > 0) {
            strTime = minutes + "分钟" + seconds + "秒";
        } else {
            strTime = second + "秒";
        }
        return strTime;
    }


    public static void main(String[] args) throws ParseException {
//        findDates(1610699070001l,1612402037297l,"yyyy-MM-dd");
//        System.out.println(System.currentTimeMillis());
//        System.out.println(System.currentTimeMillis()-(30*24*60*60*1000l));

//        System.out.println(beautifyTime(120));
//        System.out.println(beautifyTime(3600));
//        System.out.println(beautifyTime(286990));
//        System.out.println(beautifyTime(864206));

    }
}
